package com.glory.eapequipmentmonitor.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.adapter.EqpStatusListAdapter;
import com.glory.eapequipmentmonitor.bean.EqpStatusBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchManyEqpActivity extends FreeUI_BaseActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.pieChart)
    PieChart chart;
    String[] parties = new String[]{
            "RUN:200", "IDLE:122", "DOWN:6", "PAUSE:11", "REPAIR:12"
    };
    @BindView(R.id.rvList)
    RecyclerView rvList;
    private EqpStatusListAdapter eqpStatusListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_many_eqp;
    }

    @Override
    protected void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(SearchManyEqpActivity.this));
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.addItemDecoration(new DividerItemDecoration(SearchManyEqpActivity.this, 1));
        eqpStatusListAdapter = new EqpStatusListAdapter(null);
        eqpStatusListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpStatusListAdapter.isFirstOnly(true);
        rvList.setAdapter(eqpStatusListAdapter);

        List<EqpStatusBean> list = new ArrayList<>();
        list.add(new EqpStatusBean("RUN", "200"));
        list.add(new EqpStatusBean("IDLE", "122"));
        list.add(new EqpStatusBean("DOWN", "6"));
        list.add(new EqpStatusBean("PAUSE", "11"));
        list.add(new EqpStatusBean("REPAIR", "12"));
        eqpStatusListAdapter.setNewData(list);
    }

    @Override
    protected void initData() {
        chart.setUsePercentValues(true);
        chart.getDescription().setEnabled(false);
//        chart.setExtraOffsets(5, 10, 5, 5);
        chart.setDragDecelerationFrictionCoef(0.95f);

        chart.setCenterText("351\n总设备(台)");
        chart.setCenterTextColor(R.color.free_ui_dark_gold_text_gray);
        chart.setCenterTextSize(14);
        chart.setDrawHoleEnabled(true);
        chart.setHoleColor(Color.WHITE);

        chart.setTransparentCircleColor(Color.WHITE);
        chart.setTransparentCircleAlpha(110);

        chart.setHoleRadius(58f);
        chart.setTransparentCircleRadius(61f);

        chart.setDrawCenterText(true);

        chart.setRotationAngle(0);
        // enable rotation of the chart by touch
        chart.setRotationEnabled(true);
        chart.setHighlightPerTapEnabled(true);
        // chart.setUnit(" €");
        // chart.setDrawUnitsInChart(true);

        // add a selection listener
//        chart.setOnChartValueSelectedListener(this);


        chart.animateY(1400, Easing.EaseInOutQuad);
        // chart.spin(2000, 0, 360);

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(2f);
        l.setYEntrySpace(8f);
        l.setFormSize(14);
        l.setTextColor(R.color.free_ui_dark_gold_text_gray); //图例文字的颜色
        l.setTextSize(14);

        // entry label styling
        chart.setEntryLabelColor(R.color.free_ui_dark_gold_text_gray);
        chart.setEntryLabelTextSize(12f);
        chart.setDrawEntryLabels(false);
        setData(4, 10);
    }

    private void setData(int count, float range) {
        ArrayList<PieEntry> entries = new ArrayList<>();
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        entries.add(new PieEntry(200,
                parties[0]));
        entries.add(new PieEntry(122,
                parties[1]));
        entries.add(new PieEntry(6,
                parties[2]));
        entries.add(new PieEntry(11,
                parties[3]));
        entries.add(new PieEntry(12,
                parties[4]));
        PieDataSet dataSet = new PieDataSet(entries, "");

        dataSet.setDrawIcons(false);
        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);

        // add a lot of colors

        ArrayList<Integer> colors = new ArrayList<>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter(chart));
        data.setValueTextSize(10f);
        data.setValueTextColor(R.color.free_ui_dark_gold_text_gray);
        chart.setData(data);

        // undo all highlights
        chart.highlightValues(null);

        chart.invalidate();
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备状态总览");
        freeUI_titleView.setRightListener(R.drawable.refresh, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }
}
